package com.archstoday.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.archstoday.BaseApp;
import com.archstoday.R;
import com.archstoday.activity.ArticleListActivity;
import com.archstoday.activity.CommentActivity;
import com.archstoday.activity.EditUserActivity;
import com.archstoday.activity.FeedbackActivity;
import com.archstoday.activity.InvitationActivity;
import com.archstoday.activity.LoginActivity;
import com.archstoday.activity.MessageActivity;
import com.archstoday.activity.MyFollowActivity;
import com.archstoday.activity.SendArticleActivity;
import com.archstoday.activity.SettingActivity;
import com.archstoday.activity.SubscribeActivity;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.UserInfoEntity;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ToolUtils;
import com.base.http.common.BaseHttpClient;
import com.base.utils.ImageUtil;
import com.base.utils.LogUtil;
import com.base.utils.MySharedPreferencesMgr;
import com.base.utils.StringUtil;
import com.bumptech.glide.Glide;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 用户界面
 */
public class UserFragment extends AsBaseFragment {

    private CircleImageView mHeadIcon;
    private TextView mNameTv, mFollowTv, mCommentTv, mArticleTv;
    private LinearLayout mUserLayout;

    private UserInfoEntity.DataBean dataBean;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user, null);
        mHeadIcon = (CircleImageView) view.findViewById(R.id.user_head_icon);
        mNameTv = (TextView) view.findViewById(R.id.user_name_tv);
        mFollowTv = (TextView) view.findViewById(R.id.user_follow_tv);
        mCommentTv = (TextView) view.findViewById(R.id.user_comment_tv);
        mArticleTv = (TextView) view.findViewById(R.id.user_article_tv);
        mUserLayout = (LinearLayout) view.findViewById(R.id.user_layout);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!StringUtil.isEmpty(MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""))) {
            getInfo();
            mUserLayout.setVisibility(View.VISIBLE);
        } else {
            mUserLayout.setVisibility(View.GONE);

            mNameTv.setText(R.string.user_login);
            mHeadIcon.setImageResource(R.mipmap.user_header_icon);
        }

        dataBean = OAuthUtils.getInstance().getUserInfo();
        if (dataBean != null) {
            if (dataBean.getMobileNo() != null) {
                String mobile = dataBean.getMobileNo();
                StringBuffer sb = new StringBuffer(mobile);
                sb.replace(3, 7, "****");
                mNameTv.setText(String.valueOf(sb.toString()));
            }

            if (dataBean.getUserImg() != null) {
                Glide.with(getActivity()).load(dataBean.getUserImg()).
                        error(R.mipmap.user_header_icon).into(mHeadIcon);
            }
        }

    }

    @Override
    protected void initData(Bundle bundle) {
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.user_head_icon);
        setOnClickListener(R.id.user_login_btn);
        setOnClickListener(R.id.user_feedback_btn);
        setOnClickListener(R.id.user_invitation_btn);
        setOnClickListener(R.id.user_follow_btn);
        setOnClickListener(R.id.user_comment_btn);
        setOnClickListener(R.id.setting_btn);
        setOnClickListener(R.id.user_article_btn);
        setOnClickListener(R.id.user_write_article_btn);
        setOnClickListener(R.id.user_take_btn);
        setOnClickListener(R.id.user_message_btn);
    }

    @Override
    public void treatClickEvent(int id) {
        Intent intent;
        switch (id) {
            case R.id.user_login_btn:
            case R.id.user_head_icon:
                if (StringUtil.isEmpty(MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""))) {
                    intent = new Intent(mAct, LoginActivity.class);
                    startActivity(intent);
                } else {
//                    intent = new Intent(mAct, UserInfoActivity.class);
//                    intent.putExtra("isMe", true);
                    intent = new Intent(mAct, EditUserActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.user_invitation_btn:
                intent = new Intent(mAct, InvitationActivity.class);
                startActivity(intent);
                break;
            case R.id.user_feedback_btn:
                intent = new Intent(mAct, FeedbackActivity.class);
                startActivity(intent);
                break;
            case R.id.user_follow_btn:
                intent = new Intent(mAct, MyFollowActivity.class);
                startActivity(intent);
                break;
            case R.id.user_comment_btn:
                intent = new Intent(mAct, CommentActivity.class);
                startActivity(intent);
                break;
            case R.id.setting_btn:
                intent = new Intent(mAct, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.user_article_btn:
                intent = new Intent(mAct, ArticleListActivity.class);
                intent.putExtra("isMe", true);
                startActivity(intent);
                break;
            case R.id.user_write_article_btn:
                intent = new Intent(mAct, SendArticleActivity.class);
                if (dataBean != null) {
                    intent.putExtra("nick", dataBean.getName());
                }
                startActivity(intent);
                break;
            case R.id.user_take_btn:
                intent = new Intent(mAct, SubscribeActivity.class);
                startActivity(intent);
                break;
            case R.id.user_message_btn:
                intent = new Intent(mAct, MessageActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void getInfo() {
        initParameter();
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("token", MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, ""));

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.GET_USER_INFO),
                AsRes.GET_USER_INFO, mParams, UserInfoEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        if (client.getUrlIdentifier() == AsRes.GET_USER_INFO) {
            UserInfoEntity entity = (UserInfoEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                ImageUtil.setCropBitmap(mAct, mHeadIcon,
                        entity.getData().getUserImg(), R.mipmap.user_header_icon);

                String mobile = entity.getData().getMobileNo();
                StringBuffer sb = new StringBuffer(mobile);
                sb.replace(3, 7, "****");

                mNameTv.setText(sb.toString());
                mFollowTv.setText(String.valueOf(entity.getData().getLikes()));
                mCommentTv.setText(String.valueOf(entity.getData().getComments()));
                LogUtil.log_e("HX", "success==========>"+entity.getData().getArticles());
                mArticleTv.setText(String.valueOf(entity.getData().getArticles()));

                OAuthUtils.getInstance().setUserInfo(entity.getData());
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mAct, LoginActivity.class);
                startActivity(intent);
            }
        }
    }
}